#!/bin/bash

# label-outsource.sh
# 
# Script to find out whether a given server used by an institution is
# outsourced or in house. Note that the same server can be in house
# for some institutions and outsourced for others
# (e.g. oxmail.ox.ac.uk).
#
# Syntax
#
#    ./label-outsource.sh log-server.txt
#
# where log-server.txt is a text file with lines like
#
# "not registered" \t domain \t "host not found for domain"
# institution \t domain \t "host not found for domain"
# institution \t domain \t "no MX record"
# "not registered" \t domain \t mail server
# institution \t domain \t mail server
#
# Only the last formats isn't ignored. In that case, a tag is added
#
# institution \t domain \t mail server \t "in house"
# institution \t domain \t mail server \t mail provider domain name
#
# v0.3  10 Sep 2007
#
# Author: Ramon Casero <ramon.casero@oucs.ox.ac.uk>
# Copyright © 2007 University of Oxford
#
# This file is part of mta-tracker.
#
# mta-tracker is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# mta-tracker is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with mta-tracker.  If not, see <http://www.gnu.org/licenses/>.
#
# ChangeLog
#
# * 2008-08-21 (rcasero):
#
#   ** Upgrade licence to GPL v3.
#
# * 2007-08-10 (rcasero): v0.3.
#
#   ** If the domain is not recognised, tag it as "unknown provider",
#   instead of mixing it with "no provider".
#
# * 2007-08-06 (rcasero): v0.2.
#
#   ** We cannot ignore not registered domains, as sometimes they do
#   have email servers.
#
#   ** Fixed my search string for "in house" email, that was nonsense.
#
#   ** Add new domains of external providers. Some institutions are
#   outsourcing to .de, .dk and .net.uk.
#
# * 2007-08-03 (rcasero): v0.1. First working version.
#
#   ** Absorb get-external-mail-providers.sh.

# syntax message
usage() {
    echo "Syntax: `basename $0` log-server.txt"
}

# domain-name mail.foo.co.uk co.uk ==> foo.co.uk
domain-name () {
    server=$1
    tag=$2
    # get server name without domain
    head=${server%.*.$tag}
    # get domain name
    echo ${server#$head.}
}

# check number of input arguments
if [ ! $# -eq 1 ]
    then
    usage
    exit 1
fi

# alias input arguments
logfile=$1

while read line
do
  # get institution name
  insti=$(echo "$line" | cut -f 1)

#   # if not a valid institution name, then skip
#   if [[ "$insti" == "not registered" ]]
#       then
#       echo "$line"; 
#       continue
#   fi

  # get server name in lower case
  server=$(echo "$line" | cut -f 3 | tr '[A-Z]' '[a-z]')

  # get server domain name
  # (if instead of a server name we get a "no MX record", etc message,
  # skip this line)
  case "$server" in
      no\ mx\ record) echo "$line	no provider"; continue ;;
      host\ not\ found\ for\ domain) 
      echo "$line	no provider"
      continue ;;
      *.ac.uk) servdom=`domain-name $server ac.uk` ;;
      *.org.uk) servdom=`domain-name $server org.uk` ;;
      *.co.uk) servdom=`domain-name $server co.uk` ;;
      *.org) servdom=`domain-name $server org` ;;
      *.net) servdom=`domain-name $server net` ;;
      *.gov.uk) servdom=`domain-name $server gov.uk` ;;
      *.com) servdom=`domain-name $server com` ;;
      *.biz) servdom=`domain-name $server biz` ;;
      *.de) servdom=`domain-name $server de` ;;
      *.dk) servdom=`domain-name $server dk` ;;
      *.net.uk) servdom=`domain-name $server net.uk` ;;
      *.uk) servdom=`domain-name $server uk` ;;
      *.edu) servdom=`domain-name $server edu` ;;
      *) echo "$line	unknown provider"; continue ;;
  esac

  # loop through domains owned by current institution
  FOUND=false
  for domain in $(grep "^$insti	" "$logfile" | cut -f 2 | sort | uniq)
    do

    # remove dot from end of domain name
    domain=${domain%.}

    if [[ $domain == $servdom ]]
	then
	FOUND=true
	break
    fi
  done

  if [[ $FOUND == "false" ]]
      then
      echo "$line	$servdom"
  else
      echo "$line	in house"
  fi
  
done < <(cat "$logfile")
